#Include g:\AHK\git-ahk-lib\Extend.ahk

#Include g:\AHK\git-ahk-lib\util\event\MouseEvent.ahk
#Include zoom.ahk
#Include baseGui.ahk
#Include DWNThumbnail.ahk

Esc:: ExitApp()


A:: {
  Show()
}

Show() {
  tb := DWMThumbnail(WinGetID('A'), '+AlwaysOnTop ToolWindow -Caption -DPIScale')
  tb.Show()

  tb.gui.GetPos(&x, &y, &w, &h)
  tb.gui.X(x).Y(y).SizeW(w).SizeH(h)

  tb.gui.BackColor := '000000'

  me := MouseEvent(tb.gui.hwnd)
  me.Register(MouseEvent.Events.LBUTTON_DOWN, MoveWin)
  me.RegisterOnCondition(MouseEvent.Events.MOUSE_WHEEL, Noop, OnWheel)
  me.Register(MouseEvent.Events.RBUTTON_UP, OnContext)


  OnContext(*) {
    m := Menu()
    m.Add '关闭', (*) => tb.gui.Destroy()
    m.Add '全屏', SwitchFullScreen
    m.Show()
  }

  SwitchFullScreen(*) {
    g := tb.gui
    if g.FullScreen() {
      WinMove(g.X(), g.Y(), g.SizeW(), g.SizeH(), g)
    } else {
      WinMove(0, 0, A_ScreenWidth, A_ScreenHeight, g)
    }
    g.FullScreen(!g.FullScreen())
  }

  OnWheel(_, up, *) {
    zoomTo(tb.gui, tb.gui.ZoomLevel() - (up ? -1 : 1))
  }


  MoveWin(*) {
    if tb.gui.FullScreen()
      return
    CoordMode "Mouse", "Screen"
    MouseGetPos(&px, &py), tb.gui.GetPos(&wx, &wy)
    dx := wx - px, dy := wy - py
    While GetKeyState("LButton", "P")
      MouseGetPos(&nx, &ny), tb.gui.Move(nx + dx, ny + dy)
    tb.gui.X(nx + dx).Y(ny + dy)
  }
}